package com.zhiyun.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionManager;

import java.io.IOException;

/**
 * @author zc
 * @Date 2025/4/26 下午5:20
 */
public class Db {
    public static SqlSessionManager sqlSessionManager;
    static {
        try {
            sqlSessionManager = SqlSessionManager.newInstance(Resources.getResourceAsReader("mybatis.xml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static <T>  T getMapper(Class<T> clazz) {
        return sqlSessionManager.getMapper(clazz);
    }
    public static void startTransaction(){
        sqlSessionManager.startManagedSession();
    }
    public static void commitTransaction(){
        sqlSessionManager.commit();
    }
    public static void rollbackTransaction(){
        sqlSessionManager.rollback();
    }
    public static void close(){
        sqlSessionManager.close();
    }
}
